from clean_obj import *
from tkinter import *

##########################################################################

def updateTextAreaInfo(clean_sys, textarea):
    textarea.config(state=NORMAL)
    textarea.delete(1.0, END)
    textarea.insert(END, clean_sys.roomlist_to_string())
    textarea.config(state=DISABLED)
    
def initDate(clean_sys, textarea):
    for room in clean_sys._roomList:
        room.initDate()

    clean_sys.calculateCleanLevel()
    clean_sys.saveListInFile("rooms.dat")
    
    updateTextAreaInfo(clean_sys, textarea)
    print("Initialisation has done")


def cleanItem(clean_sys, roomt, itemt, textarea):
    room_data = roomt.get("1.0", END)[:-1]
    item_data = itemt.get("1.0", END)[:-1]

    result = clean_sys.updateStatus(room_data,item_data)
    clean_sys.calculateCleanLevel()
    print(result)
    if result == 0:
        print("The room <",room_data,"> doesn't exist")
    elif result == 1:
        print("The item <",item_data,"> isn't in the room <",room_data,">.")
    elif result == 2:
        print("The item <",item_data,"> in the room <",room_data,"> has been cleaned.")
    else:
        print("Error for updating item status.")

    updateTextAreaInfo(clean_sys, textarea)
    clean_sys.saveListInFile("rooms.dat")


def addRoom(clean_sys, roomt, textarea):
    room_data = roomt.get("1.0", END)[:-1]

    if room_data is not None and room_data != '' and room_data != ' ':
        if clean_sys.hasRoom(room_data):
            print("This room already exists")
        else:
            room = Room(room_data)
            clean_sys.addRoom(room)
            print("The room <",room_data,"> has been added.")

            clean_sys.calculateCleanLevel()
            updateTextAreaInfo(clean_sys, textarea)
            clean_sys.saveListInFile("rooms.dat")
    

def addItem(clean_sys, roomt, itemt, textarea):
    room_data = roomt.get("1.0", END)[:-1]
    item_data = itemt.get("1.0", END)[:-1]

    if room_data is not None and room_data != '' and room_data != ' ':
        if item_data is not None and item_data != '' and item_data != ' ':
            if clean_sys.hasRoom(room_data):
                if clean_sys.hasItemOfRoom(room_data, item_data):
                    print("This item already exists")
                else:
                    clean_sys.getRoomByName(room_data).addItem(Item(item_data))
                    clean_sys.calculateCleanLevel()
                    updateTextAreaInfo(clean_sys, textarea)
                    clean_sys.saveListInFile("rooms.dat")
                    print("The item <",item_data,"> in <",room_data,"> has been added.")
            else:
                room = Room(room_data)
                room.addItem(Item(item_data))
                clean_sys.addRoom(room)
                clean_sys.calculateCleanLevel()
                print("The item <",item_data,"> and the room <",room_data,"> has been added.")

                updateTextAreaInfo(clean_sys, textarea)
                clean_sys.saveListInFile("rooms.dat")


def removeRoom(clean_sys, roomt, textarea):
    room_data = roomt.get("1.0", END)[:-1]

    if room_data is not None and room_data != '' and room_data != ' ':
        if clean_sys.hasRoom(room_data):
            clean_sys.removeRoom(room_data)
            print("Room <",room_data,"> has been deleted.")
            clean_sys.calculateCleanLevel()
            updateTextAreaInfo(clean_sys, textarea)
            clean_sys.saveListInFile("rooms.dat")
        else:
            print("This room dosn't exist")


def removeItem(clean_sys, roomt, itemt, textarea):
    room_data = roomt.get("1.0", END)[:-1]
    item_data = itemt.get("1.0", END)[:-1]

    if room_data is not None and room_data != '' and room_data != ' ':
        if not clean_sys.hasRoom(room_data):
            print("The room <",room_data,"> doesn't exist")
        else:
            if not clean_sys.getRoomByName(room_data).hasItem(item_data):
                print("The item <",item_data,"> doesn't exist in the room <",room_data,">")
            else:
                clean_sys.getRoomByName(room_data).removeItemByName(item_data)
                print("The item <",item_data,"> in the room <",room_data,"> has been deleted.")
                clean_sys.calculateCleanLevel()
                updateTextAreaInfo(clean_sys, textarea)
                clean_sys.saveListInFile("rooms.dat")

#########################################################################################################

# read data from file
cs = CleanSystem()
cs.readListFromFile('rooms.dat')
cs.calculateCleanLevel()

# creer une interface graphique
root = Tk()
root.geometry("800x600")

lab_welcome = Label(root, text="Welcome! It's time to do some cleaning!")
lab_welcome.pack()

info_text_frame = Frame(root)
info_text_frame.pack()
scroll_bar = Scrollbar(info_text_frame)
info_text = Text(info_text_frame, height=24, width=80, bg="black", fg="white")
scroll_bar.pack(side=RIGHT, fill=Y)
info_text.pack(side=LEFT, fill=Y)
scroll_bar.config(command=info_text.yview)
info_text.config(yscrollcommand=scroll_bar.set)

oper_frame = Frame(root)
oper_frame.pack()

oper_left_frame = Frame(oper_frame)
oper_left_frame.pack(side=LEFT, pady=20, padx=20)
initDateButton = Button(oper_left_frame, text="Initiate", command=lambda:initDate(cs, info_text))
initDateButton.pack()

oper_middle_frame = Frame(oper_frame)
oper_middle_frame.pack(side=LEFT, pady=20, padx=20)
lab_room = Label(oper_middle_frame, text="Room name:")
lab_room.pack()
room_text = Text(oper_middle_frame, height=1, width=20)
room_text.pack()

oper_right_frame = Frame(oper_frame)
oper_right_frame.pack(side=LEFT, pady=20, padx=20)
lab_item = Label(oper_right_frame, text="Item name:")
lab_item.pack()
item_text = Text(oper_right_frame, height=1, width=20)
item_text.pack()

oper_lower_frame = Frame(root)
oper_lower_frame.pack(pady=20, padx=20)
updateButton = Button(oper_lower_frame, text="CleanItem", command=lambda:cleanItem(cs, room_text, item_text, info_text))
updateButton.pack(side=LEFT, padx=20)
addRoomButton = Button(oper_lower_frame, text="AddRoom", command=lambda:addRoom(cs, room_text, info_text))
addRoomButton.pack(side=LEFT, padx=20)
addItemButton = Button(oper_lower_frame, text="AddItem", command=lambda:addItem(cs, room_text, item_text, info_text))
addItemButton.pack(side=LEFT, padx=20)
removeRoomButton = Button(oper_lower_frame, text="RemoveRoom", command=lambda:removeRoom(cs, room_text, info_text))
removeRoomButton.pack(side=LEFT, padx=20)
removeItemButton = Button(oper_lower_frame, text="RemoveItem", command=lambda:removeItem(cs, room_text, item_text, info_text))
removeItemButton.pack(side=LEFT, padx=20)


# ajouter les texts dans le textarea
info_text.config(state=NORMAL)
info_text.insert(END, cs.roomlist_to_string())
info_text.config(state=DISABLED)

root.mainloop()


